package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_TopicDao;
import com.hhit.collaborative_learning.dao.UserDao;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicDaoImpl;
import com.hhit.collaborative_learning.dao.impl.UserDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.DateUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class GetForumBaseServlet
 */
@WebServlet("/community/GetForumBaseServlet")
public class GetForumBaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetForumBaseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Date tstart=new Date();
		Date tend=new Date();
		Date ystart=new Date();
		Date yend=new Date();
		tstart=DateUtil.getTodayStart(0);
		tend=DateUtil.getTodayEnd(0);
		ystart=DateUtil.getTodayStart(-1);
		yend=DateUtil.getTodayEnd(-1);
		Connection con = DBAccess.getInstance().getConn();
		BBS_TopicDao td=new BBS_TopicDaoImpl();
		UserDao ud=new UserDaoImpl();
		int uNum=ud.getcount(con, null);
		int allNum=td.getcount(con,null);
		int tNum=td.getcount(con, "'"+tstart.toString()+"'<CreatedOn<'"+tend.toString()+"'");
		int yNum=td.getcount(con, "'"+ystart.toString()+"'<CreatedOn<'"+yend.toString()+"'");
		JSONObject jobj = new JSONObject();
		jobj.put("uNum", uNum);
		jobj.put("allNum", allNum);
		jobj.put("tNum", tNum);
		jobj.put("yNum", yNum);
		try {
			ResponseUtil.write(response, jobj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("respons object :" + jobj.toString());
			e.printStackTrace();
		}finally {
			DBAccess.getInstance().close(con, null, null);
			System.out.println("respons object :" + jobj.toString());
		}
		
	}

}
